<template>
  <div class="goods-image">
    <!-- middle之前放置大图盒子 -->
    <div
      class="large"
      v-show="showFlag"
      :style="[
        {
          backgroundImage: `url(${images[curIndex]})`,
          backgroundPositionX: positionX + 'px',
          backgroundPositionY: positionY + 'px',
        },
      ]"
    ></div>
    <div class="middle" ref="target">
      <img :src="images[curIndex]" alt="" />
      <!-- 蒙层容器 -->
      <div
        class="layer"
        :style="{ left: left + 'px', top: top + 'px' }"
        v-if="showFlag"
      ></div>
    </div>
    <ul class="small">
      <li
        v-for="(img, i) in images"
        :key="img"
        @mouseenter="mouseEnterFn(i)"
        :class="{ active: i === curIndex }"
      >
        <img :src="img" alt="" />
      </li>
    </ul>
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    images: {
      type: Array,
      default: () => []
    }
  },
  setup () {
    // 实现鼠标移入交互
    const curIndex = ref(0)
    function mouseEnterFn (i) {
      curIndex.value = i
    }
    // 获取鼠标相对坐标值
    const target = ref(null)
    // 控制是否显示 false代表不显示 (直接使用isOutside 会有闪动bug)
    const showFlag = ref(false)
    // elementX 鼠标基于容器左上角X轴偏移
    // elementY 鼠标基于容器左上角Y轴偏移
    // isOutside 鼠标是否在模板容器外
    // 三个参数都是ref对象
    const { elementX, elementY, isOutside } = useMouseInElement(target)
    const left = ref(0) // 滑块距离左侧的距离
    const top = ref(0) // 滑块距离顶部的距离
    const positionX = ref(0)
    const positionY = ref(0)
    watch([elementX, elementY, isOutside], () => {
      showFlag.value = !isOutside.value

      // 只有进入到容器中才开始做移动判断
      if (isOutside.value) {
        return false
      }
      // 横向控制
      if (elementX.value < 100) {
        // left = 0
        left.value = 0
      }
      if (elementX.value > 300) {
        // left = 200
        left.value = 200
      }

      if (elementX.value > 100 && elementX.value < 300) {
        left.value = elementX.value - 100
      }
      // 纵向控制
      if (elementY.value < 100) {
        top.value = 0
      }
      if (elementY.value > 300) {
        top.value = 200
      }

      if (elementY.value > 100 && elementY.value < 300) {
        top.value = elementY.value - 100
      }
      // 控制背景图：1.方向相反  2.比例1:2
      positionX.value = -left.value * 2
      positionY.value = -top.value * 2
    })

    return {
      curIndex,
      mouseEnterFn,
      target,
      top,
      left,
      positionX,
      positionY,
      showFlag
    }
  }
}
</script>
<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    z-index: 500;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
